<?php
class Managearticles_Data_Editform {
	
	private $form;
	private $database;
	private $secure;
	
	public function __construct(){
		$this->form = new Core_Driver_Firstform();
		$this->database = new Core_Driver_MysqlImproved();
		$this->secure = new Core_Data_Secure();
	}
	
	private function getData(array $getVars){
		$this->database->connect();
		$page = $getVars['name'];
		$page = $this->database->escape($page);
		$this->database->prepare(
			"
				SELECT * FROM page_settings, page_meta_values, page_content_values WHERE page_settings.page_name = page_meta_values.page_name AND page_settings.page_name = page_content_values.page_name AND page_settings.page_name = '$page' LIMIT 1;
				"
		);
		$this->database->query();
		$data = $this->database->fetch('array');
		return $data;
	}
	
	public function getForm(array $getVars){
		$data = $this->getData($getVars);
		$access_levels = $this->getAccessLevel();
		$categories = $this->getCategory();
		$article['content'] = $this->form->startForm(
			array(
			"id" => "pageEditForm",
					"name" => "pageEditForm",
					"method" => "post",
					"action" => SERVER_ROOT . "?managearticles&page=update"
		)
		);
		$article['content'] .= $this->form->inputForm(
			array(
			"type" => "hidden",
					"name" => "oldPageName",
					"value" => $data['page_name']
		),
				null
		);
		$article['content'] .= ADD_ARTICLES_MESSAGE;
		$article['content'] .= "<ul>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "page_name"
				),
				EDIT_ARTICLES_PAGE_NAME,
				null
		);
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "pageName",
						"name" => "pageName",
						"value" => $data['page_name'],
						"type" => "text"
				),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
			array(
			"for" => "page_title"
		),
				EDIT_ARTICLES_PAGE_TITLE,
				null
		);
		$article['content'] .= $this->form->inputForm(
			array(
			"id" => "pageTitle",
					"name" => "pageTitle",
					"value" => $data['page_title'],
					"type" => "text"
		),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageMetaTitle"
				),
				EDIT_ARTICLES_PAGE_META_TITLE,
				null
		);
		$article['content'] .= $this->form->inputForm(
				array(
						"id" => "pageMetaTitle",
						"name" => "pageMetaTitle",
						"value" => $data['page_meta_title'],
						"type" => "text"
				),
				"required autofocus"
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "avalaible"
				),
				EDIT_ARTICLES_PAGE_ACTIVE,
				null
		);
		$article['content'] .= $this->form->selectForm(
				array(
						"id" => "available",
						"name" => "available"
				),
				null,
				array(
						"1" => "true",
						"0" => "false"
				)
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "accessLevel"
				),
				EDIT_ARTICLES_PAGE_ACCESS_LEVEL,
				null
		);
		$article['content'] .= $this->form->selectForm(
				array(
						"id" => "accessLevel",
						"name" => "accessLevel"
				),
				null,
				$access_levels
		);
		$article['content'] .= "</li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "category"
				),
				EDIT_ARTICLES_PAGE_CATEGORY,
				null
		);
		$article['content'] .= $this->form->selectForm(
				array(
						"id" => "category",
						"name" => "category"
				),
				null,
				$categories
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
			array(
			"for" => "pageKeywords"
		),
				EDIT_ARTICLES_PAGE_KEYWORDS,
				null
		);
		$article['content'] .= $this->form->textareaForm(
			array(
			"name" => "pageKeywords",
					"maxlength" => "1000",
					"id" => "pageKeywords",
					"style" => "height: 5 em;"
		),
				"required",
				$data['page_meta_keywords']
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
			array(
			"for" => "pageMetaDescription"
		),
				EDIT_ARTICLES_PAGE_META_DESCRIPTION
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageMetaDescription",
						"maxlength" => "1000",
						"id" => "pageMetadescription",
						"style" => "height: 5 em"
						
				),
				"required",
				$data['page_meta_description']
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageDescription"
				),
				EDIT_ARTICLES_PAGE_DESCRIPTION
		);
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageDescription",
						"maxlentgth" => "1000",
						"id" => "pageDescription",
						"style" => "height: 5 em;"
				),
				"required",
				$data['page_description']
		);
		$article['content'] .= "</li>";
		$article['content'] .= "<li>";
		$article['content'] .= $this->form->labelForm(
				array(
						"for" => "pageContent"
				),
				EDIT_ARTICLES_PAGE_CONTENT
		);
		$article['content'] .= "<br>";
		$article['content'] .= $this->form->textareaForm(
				array(
						"name" => "pageContent",
						"maxlength" => "100000",
						"id" => "pageContent",
						"style" => "height: 5em"
				),
				"required",
				$data['page_content']
		);
		$article['content'] .= "</li>";
		$article['content'] .= "</ul>";
		$article['content'] .= '<div class="buttons">';
		$article['content'] .= $this->form->inputForm(
			array(
			"id" => "submitForm",
					"name" => "submit",
					"type" => "submit",
					"value" => EDIT_ARTICLES_BUTTON
		),
				null
		);
		$article['content'] .= "</div>";
		$article['content'] .= $this->form->endForm();
		return $article;
	}
	
	private function getAccessLevel(){
		$this->database->connect();
		$this->database->prepare(
				"
				select * from privileges order by privileges_types asc;
				"
		);
		$this->database->query();
		$access_levels = array();
		while($result = $this->database->fetch('array')){
			$access_levels[$result['privileges_types']] = "(".$result['privileges_types'].") ".$result['privileges_name'];
		}
		return $access_levels;
	}
	
	private function getCategory(){
		$this->database->connect();
		$this->database->prepare(
				"
				select * from category_settings;
				"
		);
		$this->database->query();
		$categories = array();
		while($categoryData = $this->database->fetch('array')){
			$categories[$categoryData['category_name']] = $categoryData['category_name'];
		}
		return $categories;
	}
}